package com.ruoyi.pingjiao.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pingjiao.domain.Teacher;
import com.ruoyi.pingjiao.service.ITeacherService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教师管理Controller
 *
 * @author 胡浩鹏
 * @date 2022-10-15
 */
@Api("教师管理")
@RestController
@RequestMapping("/score/teacher")
public class TeacherController extends BaseController
{
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询教师管理列表
     */
    @PreAuthorize("@ss.hasPermi('score:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(Teacher teacher)
    {
        startPage();
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        return getDataTable(list);
    }

    /**
     * 查询教师管理列表不带分页
     */
    @PreAuthorize("@ss.hasPermi('score:teacher:list')")
    @GetMapping("/listSimple")
    public TableDataInfo listSimple(Teacher teacher)
    {
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        return getDataTable(list);
    }

    @ApiOperation("批量导入教师信息，通过Excel文件")
    @PreAuthorize("@ss.hasAnyRoles('systemAdmin,collegeAdmin')")
    @Log(title = "教师管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        List<Teacher> teacherList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = teacherService.importTeacher(teacherList, operName);
        return success(message);
    }

    /**
     * 导出教师管理列表
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:teacher:export')")
    @Log(title = "教师管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Teacher teacher)
    {
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        util.exportExcel(response, list, "教师管理数据");
    }
    @PreAuthorize("@ss.hasPermi('pingjiao:teacher:export')")
    @Log(title = "教师列表模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export1")
    public void export1(HttpServletResponse response, Teacher teacher)
    {
//        List<Teacher> list = teacherService.selectTeacherList(teacher);
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        util.exportExcel(response, null, "教师管理数据");
    }
    /**
     * 获取教师管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('score:teacher:query')")
    @GetMapping(value = "/{teacherId}")
    public AjaxResult getInfo(@PathVariable("teacherId") Long teacherId)
    {
        return AjaxResult.success(teacherService.selectTeacherByTeacherId(teacherId));
    }
     @GetMapping("/Info/{userId}")
     public AjaxResult getInfoByUserId(@PathVariable Long userId){
        return AjaxResult.success(teacherService.getInfoByUserId(userId));
     }
    /**
     * 获取当前登录教师管理的课程信息
     */
    @ApiOperation("获取当前登录教师管理的上课班级")
    @PreAuthorize("@ss.hasPermi('score:teacher:getCourseListForOneTeacher')")
    @GetMapping(value = "/getCourseListForOneTeacher")
    public AjaxResult selectTeacherWithCourseByTeacherId()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return AjaxResult.success(teacherService.selectTeacherWithCourseByTeacherId(user.getStuOrTeaId()));
    }

    /**
     * 获取当前登录教师管理的课程信息
     * 测试
     */
    @ApiOperation("根据TeacherID获取上课班级信息")
    @ApiImplicitParams({@ApiImplicitParam(name= "teacherId", value = "教师ID", required = true, dataType = "Long")
    })
    @PreAuthorize("@ss.hasPermi('pingjiao:teacher:query')")
    @GetMapping(value = "/getCourseList/{teacherId}")
    public AjaxResult selectTeacherWithCourseByTeacherId(@PathVariable("teacherId") Long teacherId)
    {
        return AjaxResult.success(teacherService.selectTeacherWithCourseByTeacherId(teacherId));
    }

    /**
     * 获取当前登录教师管理的课程下行政班级信息
     */
    @ApiOperation("获取当前登录教师管理的某个课程下行政班级信息")
    @ApiImplicitParams({@ApiImplicitParam(name= "courseclassId", value = "上课班级ID", required = true, dataType = "Long")
    })
    @PreAuthorize("@ss.hasPermi('pingjiao:teacher:selectMetaclass')")
    @GetMapping(value = "/getMetaclassList/{courseclassId}")
    public AjaxResult selectMetaclassByTeacherIdAndCourseclassId(@PathVariable("courseclassId") Long courseclassId)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return AjaxResult.success(teacherService.selectMetaclassByTeacherIdAndCourseclassId(courseclassId,user.getStuOrTeaId()));
    }

    /**
     * 获取当前登录教师管理的课程下行政班级信息
     * 测试
     */
    @ApiOperation("根据教师ID和上课班级ID获取行政班级信息")
    @ApiImplicitParams({@ApiImplicitParam(name= "courseclassId", value = "上课班级ID", required = true, dataType = "Long")
    })
    @PreAuthorize("@ss.hasPermi('pingjiao:teacher:query')")
    @GetMapping(value = "/getMetaclassList/{teacherId}/{courseclassId}")
    public AjaxResult selectMetaclassByTeacherIdAndCourseclassId(@PathVariable("courseclassId") Long courseclassId, @PathVariable Long teacherId) {

        return AjaxResult.success(teacherService.selectMetaclassByTeacherIdAndCourseclassId(courseclassId,
                teacherId));
    }


    /**
     * 新增教师管理
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:teacher:add')")
    @Log(title = "教师管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Teacher teacher)
    {
        Teacher t = new Teacher();
        t.setTeacherNo(teacher.getTeacherNo());
        if(!UserConstants.UNIQUE.equals(String.valueOf(teacherService.selectTeacherList(t).size()))){
            return AjaxResult.error("新增教师'" + teacher.getRealName() + "'失败，工号"+teacher.getTeacherNo()+"已存在");
        }
        return toAjax(teacherService.insertTeacher(teacher));
    }

    /**
     * 修改教师管理
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:teacher:edit')")
    @Log(title = "教师管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Teacher teacher)
    {
        return toAjax(teacherService.updateTeacher(teacher));
    }

    /**
     * 删除教师管理
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:teacher:remove')")
    @Log(title = "教师管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{teacherIds}")
    public AjaxResult remove(@PathVariable Long[] teacherIds)
    {
        return toAjax(teacherService.deleteTeacherByTeacherIds(teacherIds));
    }



    /**
     * 教师获取自己本学期参加的活动
     * @author  hpf
     * @return 活动列表
     */
    @ApiOperation("教师获取当前学期的评价活动")
    @ApiImplicitParams({@ApiImplicitParam(name= "term", value = "学期", required = true, dataType = "String")
    })
    @PreAuthorize("@ss.hasPermi('pingjiao:teacher:selectActivity')")
    @GetMapping("/selectActivity/{term}")
    public AjaxResult selectActivity(@PathVariable String term){
        if(term.equals("0")){
            term=sysConfigService.selectConfigByKey("currentTerm");//0表示当前学期，从系统中自动获取
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Teacher teacher = teacherService.selectTeacherByUserId(loginUser.getUserId());
        Long teacherId=teacher.getTeacherId();
        List<Map<String, Object>> evalMap = teacherService.selectActivityByTeacherId(teacherId,term);
        return  AjaxResult.success(evalMap);
    }
    /**
     * 教师获取自己某学期评教成绩
     * @author  hpf
     * @return 教师成绩
     */
    @ApiOperation("教师获取某活动评教成绩列表")
    @ApiImplicitParams({@ApiImplicitParam(name= "activityId", value = "活动Id", required = true, dataType = "int")
    })
    @PreAuthorize("@ss.hasPermi('pingjiao:teacher:selectPersonScore')")
    @GetMapping("/selectPersonScore/{activityId}")
    public AjaxResult selectPersonScoreByActivityId(@PathVariable Long activityId){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Teacher teacher = teacherService.selectTeacherByUserId(loginUser.getUserId());
        Long teacherId=teacher.getTeacherId();
        List<Map<String, Object>> map = teacherService.selectEvaluationScoreByTeacherId(teacherId,activityId);
        return  AjaxResult.success(map);
    }
    @GetMapping("getRank")
    public AjaxResult getRank(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Teacher teacher = teacherService.selectTeacherByUserId(loginUser.getUserId());
        Long teacherId=teacher.getTeacherId();
        List<Map<String,Object>>maps=teacherService.getRank(teacherId);
        return AjaxResult.success(maps);
    }
}
